package edu.bw.controller;

import edu.bw.common.CommonResult;
import edu.bw.dto.InsertRoleForm;
import edu.bw.dto.SelectRoleByConditionForm;
import edu.bw.dto.UpdateRoleForm;
import edu.bw.po.Role;
import edu.bw.service.RoleService;
import edu.bw.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@CrossOrigin //实现跨域请求
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/selectRoleByCondition")
    public CommonResult selectRoleByCondition(@RequestBody SelectRoleByConditionForm selectCondition) {
        PageUtils pageUtils = roleService.selectRoleByCondition(selectCondition);
        return CommonResult.ok().put("page", pageUtils);
    }

    @PostMapping("/insert")
    public CommonResult insertRole(@RequestBody InsertRoleForm insertRole) {
        Integer rows = roleService.insert(insertRole);
        return CommonResult.ok().put("rows", rows);
    }

    @PostMapping("/selectById")
    public CommonResult selectById(@RequestBody Role role) {
        List<Map<String, Object>> list = roleService.selectById(role.getId());
        return CommonResult.ok(list.get(0));
    }

    @PostMapping("/update")
    public CommonResult updateRole(@RequestBody UpdateRoleForm updateRole) {
        Integer rows = roleService.update(updateRole);
        return CommonResult.ok().put("rows", rows);
    }

    @PostMapping("/deleteRoleByIds")
    public CommonResult deleteRoleById(@RequestBody Map<String, List<Integer>> params) {
        List<Integer> ids = params.get("ids");
        Integer rows = roleService.deleteRoleByIds(ids);
        return CommonResult.ok().put("rows", rows);
    }

    @GetMapping("/selectAllRole") //前端用的get请求 所以这里用get
    public CommonResult selectAllRole() {
        List<Map<String, Object>> list = roleService.selectAllRole();
        return CommonResult.ok().put("list", list);
    }
}
